package portBroadcasting;

import com.phidgets.PhidgetException;
import com.phidgets.TemperatureSensorPhidget;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * User: Mike
 * Date: 12/11/11
 */

/**
 * Class is used for getting the temperature from a phidget via the getTempMessage method.
 *
 * Used in the Server class which is used in multi casting
 */

public class Temperature {

    private TemperatureSensorPhidget tempsensor;

    public Temperature() {
        try {
            tempsensor = new TemperatureSensorPhidget();
            System.out.println("temperature started");
            tempsensor.openAny();
            System.out.println("waiting for TemperatureSensor attachment...");
            tempsensor.waitForAttachment();
            tempsensor.setTemperatureChangeTrigger(0, 0.2);
            tempsensor.setThermocoupleType(0, TemperatureSensorPhidget.PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
            System.out.println("captured TemperatureSensor attachment.");
        } catch (PhidgetException e) {
            e.printStackTrace();
        }
    }

//    public String GetColTempC() throws PhidgetException, IOException {
//        final double calibration = 0.1;
//        double columnDC = tempsensor.getTemperature(0) - calibration;
//        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
//        DecimalFormat df = new DecimalFormat("###.###");
//        return String.valueOf(df.format(columnDC));
//    }

//    public String GetBoardTempC() throws PhidgetException, IOException {
//        final double calibration = 0.1;
//        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
//        DecimalFormat df = new DecimalFormat("###.###");
//        return String.valueOf(df.format(ambientDC));
//    }

    public String GetTempMessage() throws PhidgetException, IOException {
        final double calibration = 0.1;
        // testing phase-----------------------------------------------------
        double columnDC = tempsensor.getTemperature(0) - calibration;
        double ambientDC = tempsensor.getAmbientTemperature() - calibration;
//        double columnDC = 98.6 - calibration;
//        double ambientDC = 198.6 - calibration;
        // testing phase-----------------------------------------------------

        double columnDF = columnDC * 9.0 / 5.0 + 32.0;
        double ambientDF = ambientDC * 9.0 / 5.0 + 32.0;
        DecimalFormat df = new DecimalFormat("###.###");
        columnDC = Double.valueOf(df.format(columnDC));
        ambientDC = Double.valueOf(df.format(ambientDC));
        columnDF = Double.valueOf(df.format(columnDF));
        ambientDF = Double.valueOf(df.format(ambientDF));
        Long milliTime = System.currentTimeMillis();
        String lineToWrite = milliTime + "," + columnDC + "," + columnDF + "," + ambientDC + "," + ambientDF;
        System.out.println("time = " + new Date(System.currentTimeMillis()) + " ::: Column = (" + columnDC + "," + columnDF + ") Ambient = (" + ambientDC + "," + ambientDF + ")");
        return lineToWrite;
    }


//    public static void main(String[] args) {
//        temperature();
//    }
}
